<template>
  <div class="app-container">
    <el-form label-width="120px">
      <el-form-item label="轮播图名称">
        <el-input v-model="banner.title" />
      </el-form-item>
      <el-form-item label="轮播图排序">
        <el-input-number
          v-model="banner.sort"
          controls-position="right"
          :min="0"
        />
      </el-form-item>

      <!-- 讲师头像：TODO -->

      <!-- 讲师头像 -->
      <!-- <el-form-item label="轮播图图片"> -->
      <!-- 头衔缩略图 -->
      <!-- <pan-thumb :image="Banner.avatar" /> -->
      <!-- 文件上传按钮 -->
      <!-- <el-button
          type="primary"
          icon="el-icon-upload"
          @click="imagecropperShow = true"
          >更换头像
        </el-button> -->

      <!--
v-show：是否显示上传组件
:key：类似于id，如果一个页面多个图片上传控件，可以做区分
:url：后台上传的url地址
@close：关闭上传组件
@crop-upload-success：上传成功后的回调 -->
      <!-- <image-cropper
          v-show="imagecropperShow"
          :width="300"
          :height="300"
          :key="imagecropperKey"
          :url="BASE_API + '/eduoss/fileoss/upload/avatar'"
          field="file"
          @close="close"
          @crop-upload-success="cropSuccess"
        /> -->

      <el-form-item label="轮播图图片">
        <el-upload
          :show-file-list="false"
          :on-success="handleSuccess"
          :before-upload="beforeAvatarUpload"
          :action="BASE_API + '/eduoss/fileoss/upload/banner'"
          class="avatar-uploader"
        >
          <img :src="banner.imageUrl" width="800px" />
        </el-upload>
      </el-form-item>

      <el-form-item>
        <el-button
          :disabled="saveBtnDisabled"
          type="primary"
          @click="saveOrUpdate"
          >提交</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import bannerApi from "@/api/edu/banner/banner.js";
import ImageCropper from "@/components/ImageCropper";
import PanThumb from "@/components/PanThumb";
export default {
  components: { ImageCropper, PanThumb },
  data() {
    return {
      banner: {
        id:'',
        title: "",
        sort: 0,
        imageUrl: process.env.OSS_PATH + "/default/banner.jpeg",
        linkUrl: process.env.OSS_PATH + "/default/banner.jpeg",
      },
      saveBtnDisabled: false, // 保存按钮是否禁用
      imagecropperShow: false, //弹窗显示
      imagecropperKey: 0, //上传图片唯一表示
      BASE_API: process.env.BASE_API,
      OSS_PATH: process.env.OSS_PATH,
    };
  },
  created() {
    this.init();
  },
  methods: {
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt20M = file.size / 1024 / 1024 < 20;
      
      if (!isLt20M) {
        this.$message.error("上传头像图片大小不能超过 20MB!");
      }
      return isJPG && isLt20M;
    },
    handleSuccess(res, file) {
      console.log(res);
      this.$message({
        type: "success",
        message: "上传封面成功",
      });
      this.banner.imageUrl = res.data.url;
    },
    close() {
      this.imagecropperShow = false;
    },
    
    saveOrUpdate() {
      console.log("22")
      if (this.banner.id) {
        this.updateBanner();
      } else {
        this.saveBanner();
      }
    },
    saveBanner() {
      bannerApi.saveBanner(this.banner).then((response) => {
        this.$message({
          type: "success",
          message: "添加成功!",
        });
        this.$router.push({ path: "/banner/list" });
      });
    },
    getInfo(id) {
      bannerApi.getDetailById(id).then((response) => {
        this.banner = response.data.item;
      });
    },
    updateBanner() {
      bannerApi.updateBanner(this.banner).then((response) => {
        this.$message({
          type: "success",
          message: "修改成功!",
        });
        this.$router.push({ path: "/banner/list" });
      });
    },
    init() {
      if (this.$route.params && this.$route.params.id) {
        const id = this.$route.params.id;
        this.getInfo(id);
      } else {
        this.banner = {
          imageUrl: process.env.OSS_PATH + "/default/banner.jpeg",
          linkUrl: process.env.OSS_PATH + "/default/banner.jpeg",
        };
      }
    },
  },
  watch: {
    $route(to, from) {
      this.init();
    },
  },
};
</script>